/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package staplers;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author Richard
 */
@ManagedBean
@SessionScoped
public class OrderCTRL {
    DBHelper helper;
    List itemList;
    List orderList;
    List <Date> ordersByDate;
    String firstDate;
    int startDateMonth;
    List <String> uniqueDates=new ArrayList<String>();
    
    public OrderCTRL() {
        helper = ((UserCTRL)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("userCTRL")).getHelper();
    }
    
    public List getItemList()
    {
        return helper.getAllItems();        
        
    }
    
    public List getOrderList()
    {
         return helper.getAllOrders();        
    }
    
    public List getOrdersByDate(){
        List <Order> orders;
        orders = helper.getAllOrders();
        int j = 0;
	while (j < orders.size()) {
                DateFormat df=new SimpleDateFormat("MM-dd-yyyy");
                String s=df.format(orders.get(j).getDate());
		System.out.println(s);
		j++;
	}
        return ordersByDate;
    }
    
    public String getFirstDate(){
        List <Order> orders;
        orders = helper.getAllOrders();
        int j = 1;
        
        if(orders.get(0).getDate()!=null){
        Date temp = orders.get(0).getDate();
        while (j < orders.size()) {
            if(orders.get(j).getDate().before(temp)){
                temp=orders.get(j).getDate();
            }
               
		j++;
	}
        DateFormat df=new SimpleDateFormat("MM-dd-yyyy");
                String s=df.format(temp);
		System.out.println(s);
                firstDate=s;
        return firstDate;
        }else{
            firstDate="No date found";
            return firstDate;
        }
    }
    
    public void setStartDateMonth(int month){
        System.out.println ("The selected value is"+month);
        startDateMonth=month;
    }
    
    public int getStartDateMonth(){
        return startDateMonth;
    }
    
    public List getUniqueDates(){
        boolean match = false;
        DateFormat df=new SimpleDateFormat("MM-dd-yyyy");
        List <Order> orders = helper.getAllOrders();
        uniqueDates.clear();
        uniqueDates.add(df.format(orders.get(0).getDate()));
        int j = 0;
        while (j < orders.size()) {
            int k = 0;
            match=false;
            while(k < uniqueDates.size()){
                if(df.format(orders.get(j).getDate()).equals(uniqueDates.get(k))){
                    match=true;
                }
                k++;
            }
            if(match==false){
                uniqueDates.add(df.format(orders.get(j).getDate()));
            }
            j++;
        }
        System.out.println(uniqueDates);
        return uniqueDates;
    }
    
    
    
    
}